AR		=ar
LD		=ld
LDFLAGS	=-m elf_i386
CC		=gcc
CFLAGS	=-std=c99 -m32 -fno-builtin -fomit-frame-pointer -fno-stack-protector -nostdinc -Iinclude

QUIET = @

.c.s:
	$(QUIET)$(CC) $(CFLAGS) -S -o $*.s $<
.s.o:
	$(QUIET)$(AS) -c -o $*.o $<
.c.o:
# 生成依赖文件和目标文件
	$(QUIET)$(CC) $(CFLAGS) -MMD -MF $*.d -MT $*.o -c $< -o $*.o

LINKER = linker.lds
SRC_DIR = ./src

TARGET = main
TAR_OBJ = $(TARGET).o
TAR_DEP = $(TARGET).d
LIB = libc.a
LIBC = -L. -lc

SRCS = $(shell find $(SRC_DIR) -name "*.c")
OBJS = $(patsubst %.c, %.o, $(SRCS))
DEPS = $(patsubst %.c, %.d, $(SRCS))

default: $(TARGET)

$(TARGET): $(LIB) $(TAR_OBJ)
	$(QUIET)$(LD) $(LDFLAGS) $(TAR_OBJ) -o $@ -T $(LINKER) $(LIBC)
	$(QUIET)echo "target complete!"

$(LIB): $(OBJS)
	$(QUIET)$(AR) rcs $@ $^

# 包含依赖文件
-include $(DEPS)

clean:
	$(QUIET)rm -rf $(OBJS) $(TAR_OBJ) $(DEPS) $(TAR_DEP) $(TARGET) $(LIB)
